<?php
/*
  $Id:$
*/

  class messageStack {
    var $size = 0;

    function messageStack() {
      $this->messages = array();

      if (isset($_SESSION['messageToStack']) && $_SESSION['messageToStack']) {
        for ($i = 0, $n = sizeof($_SESSION['messageToStack']); $i < $n; $i++) {
          $this->add($_SESSION['messageToStack'][$i]['text'], $_SESSION['messageToStack'][$i]['type']);
        }
        unset($_SESSION['messageToStack']);
      }
    }

    
    function add($message, $type = 'error') {
      if ($type == 'error') {
        $this->messages[] = array('params' => 'class="message_stack message_stack_error"', 'text' => $message);
      } elseif ($type == 'warning') {
        $this->messages[] = array('params' => 'class="message_stack message_stack_warning"', 'text' => $message);
      } elseif ($type == 'success') {
        $this->messages[] = array('params' => 'class="message_stack message_stack_success"', 'text' => $message);
      } else {
        $this->messages[] = array('params' => 'class="message_stack"', 'text' => $message);
      }

      $this->size++;
    }

    function add_session($message, $type = 'error') {
      $_SESSION['messageToStack'][] = array('text' => $message, 'type' => $type);
    }

    function reset() {
      $this->messages = array();
      $this->size = 0;
    }

    function output() {
      foreach ($this->messages as $m) {
?>
  <div <?php echo $m['params'];?>><?php echo $m['text'];?></div>
<?php
      }
    }
  }
?>
